//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    endTime: '2020-11-11 23:11:11',
    circleList: [], //圆点数组
    awardList: [], //奖品数组
    colorCircleFirst: '#FFDF2F', //圆点颜色1
    colorCircleSecond: '#FE4D32', //圆点颜色2
    colorAwardDefault: '#F5F0FC', //奖品默认颜色
    colorAwardSelect: '#ffe400', //奖品选中颜色
    indexSelect: 0, //被选中的奖品index
    isRunning: false, //是否正在抽奖
    imageAward: [
      '../../../images/hb.png',
      '../../../images/hb.png', '../../../images/hb.png', '../../../images/hb.png', '../../../images/hb.png', '../../../images/hb.png', '../../../images/hb.png', '../../../images/hb.png',
    ], //奖品图片数组
    jp: '',
    jiang: false
  },

  countDown: function () {
    var that = this;
    var nowTime = new Date().getTime(); //现在时间（时间戳）
    var time = that.data.endTime;
    time = time.replace(/-/g, ':').replace(' ', ':')
    time = time.split(':')
    var time1 = new Date(time[0], (time[1] - 1), time[2], time[3], time[4], time[5])
    var endTime = new Date(time1).getTime(); //结束时间（时间戳）
    var time = (endTime - nowTime) / 1000; //距离结束的毫秒数
    // 获取天、时、分、秒
    let day = parseInt(time / (60 * 60 * 24));
    let hou = parseInt(time % (60 * 60 * 24) / 3600);
    let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
    let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
    //  console.log(day + "," + hou + "," + min + "," + sec)
    that.setData({
      day: day,
      hou: hou,
      min: min,
      sec: sec
    })
    // 每1000ms刷新一次
    if (time > 0) {
      that.setData({
        countDown: true
      })
      setTimeout(this.countDown, 1000);
    } else {
      that.setData({
        countDown: false
      })
    }
  },

  onLoad: function () {
    var _this = this;
    _this.countDown()

    //圆点设置
    var leftCircle = 7.5;
    var topCircle = 7.5;
    var circleList = [];
    for (var i = 0; i < 24; i++) {
      if (i == 0) {
        topCircle = 15;
        leftCircle = 15;
      } else if (i < 6) {
        topCircle = 7.5;
        leftCircle = leftCircle + 102.5;
      } else if (i == 6) {
        topCircle = 15
        leftCircle = 620;
      } else if (i < 12) {
        topCircle = topCircle + 94;
        leftCircle = 620;
      } else if (i == 12) {
        topCircle = 565;
        leftCircle = 620;
      } else if (i < 18) {
        topCircle = 570;
        leftCircle = leftCircle - 102.5;
      } else if (i == 18) {
        topCircle = 565;
        leftCircle = 15;
      } else if (i < 24) {
        topCircle = topCircle - 94;
        leftCircle = 7.5;
      } else {
        return
      }
      circleList.push({
        topCircle: topCircle,
        leftCircle: leftCircle
      });
    }
    this.setData({
      circleList: circleList
    })

    //圆点闪烁
    setInterval(function () {
      if (_this.data.colorCircleFirst == '#FFDF2F') {
        _this.setData({
          colorCircleFirst: '#FE4D32',
          colorCircleSecond: '#FFDF2F',
        })
      } else {
        _this.setData({
          colorCircleFirst: '#FFDF2F',
          colorCircleSecond: '#FE4D32',
        })
      }
    }, 500) //设置圆点闪烁的效果

    //奖品item设置
    var awardList = [];
    //间距,怎么顺眼怎么设置吧.
    var topAward = 25;
    var leftAward = 25;
    for (var j = 0; j < 8; j++) {
      if (j == 0) {
        topAward = 25;
        leftAward = 25;
      } else if (j < 3) {
        topAward = topAward;
        //166.6666是宽.15是间距.下同
        leftAward = leftAward + 166.6666 + 15;
      } else if (j < 5) {
        leftAward = leftAward;
        //150是高,15是间距,下同
        topAward = topAward + 150 + 15;
      } else if (j < 7) {
        leftAward = leftAward - 166.6666 - 15;
        topAward = topAward;
      } else if (j < 8) {
        leftAward = leftAward;
        topAward = topAward - 150 - 15;
      }
      var imageAward = this.data.imageAward[j];
      awardList.push({
        topAward: topAward,
        leftAward: leftAward,
        imageAward: imageAward
      });
    }
    this.setData({
      awardList: awardList
    })
  },

  //开始抽奖
  startGame: function () {
    if (this.data.isRunning) return
    this.setData({
      isRunning: true
    })
    var _this = this;
    var indexSelect = 0
    var i = 0;
    var timer = setInterval(function () {
      indexSelect++;
      var random = Math.random() * 100; //转盘
      // console.log(random)
      i += random;
      if (i > 1000) {
        //去除循环
        clearInterval(timer)
        //获奖提示
        var phone = wx.getStorageSync('jljphone')
        var gonghao = wx.getStorageSync('gonghao')
        wx.request({ //发送请求
          url: 'https://zg99.offcn.com/index/choujiang/getcjusers?actid=42483&callback=?', //接收url
          data: {
            phone: phone,
            limits: 100
          }, //接收参数
          success(res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            }
            console.log(theJson)
            if (theJson.status === 2) {
              wx.request({ //发送请求
                url: 'https://zg99.offcn.com/index/choujiang/lottery?actid=42483&callback=?', //接收url
                data: {
                  phone: phone,
                  gonghao:gonghao
                }, //接收参数
                success(res) {
                  var jsonStr = res.data;
                  jsonStr = jsonStr.replace("(", "");
                  jsonStr = jsonStr.replace(")", "");
                  if (typeof jsonStr != 'object') {
                    jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                    var theJson = JSON.parse(jsonStr);
                  }
                  console.log(theJson)
                  wx.setStorageSync('prizename', theJson.prizename)
                  // _this.setData({
                  //   jiang: true,
                  //   isRunning: false
                  // })
                  wx.navigateTo({
                    url: '../result/result',
                  })

                }
              })

            } else if (theJson.status === 1) {
              wx.showModal({
                title: '您已经抽过奖了',
                content: '您获得了' + theJson.lists[0].prizename,
                showCancel: false, //去掉取消按钮
                success: function (res) {
                  if (res.confirm) {
                    _this.setData({
                      isRunning: false
                    })
                  }

                  wx.navigateTo({
                    url: '../list/list',
                  })
                }
              })
            } else {
              wx.showModal({
                title: '次数不足',
                content: '您已经进行过抽奖了！',
                showCancel: false, //去掉取消按钮
                success: function (res) {
                  if (res.confirm) {
                    _this.setData({
                      isRunning: false
                    })
                  }
                  wx.navigateTo({
                    url: '../list/list',
                  })
                }
              })
            }
          }
        })
      }
      indexSelect = indexSelect % 8;
      _this.setData({
        indexSelect: indexSelect
      })
    }, (200 + i))
  }
})